<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class PartitionController extends Controller
{
   /*
        显示社区 分区添加页面
   */
    public function getAdd()
    {
        //查询所有的用户
        $user = DB::table('user')->get();

        //解析模板
        return view('admin.partition.add',['user'=>$user]);
    }

    /*
        执行添加
    */
    public function postInsert(Request $request)
    {
        $data = $request->only(['partition_name','user_id']);
        //分区名称不能为空
        if(empty($data['partition_name'])){
            return back()->with('error','分区名称不能为空');
        }
        //查询分区是否已经存在
        $partition = DB::table('partition')->where('partition_name',$data['partition_name'])->first();
        //分区名称唯一
        if($partition){
            return back()->with('error','该分区名称已存在,请重新输入!');
        }

        //执行添加
        $res = DB::table('partition')->insert($data);
        if($res){
            return redirect('/admin/partition/index')->with('success','分区添加成功');
        }else{
            return back()->with('error','分区添加失败');
        }
    }

    /*
        分区列表
    */
    public function getIndex(Request $request)
    {
        //每页显示几条
        $num = $request->input('num',10);

        //判断用户是否需要搜索
        if(!empty($request->input('keywords'))){
            $partition = DB::table('partition')
                            ->join('user','partition.user_id','=','user.id')
                            ->select('partition.*','user.username')
                            ->where('partition_name','like','%'.$request->input('keywords').'%')
                            ->paginate($num);
        }else{
            //查询所有的分区
            $partition = DB::table('partition')
                            ->join('user','partition.user_id','=','user.id')
                            ->select('partition.*','user.username')
                            ->paginate($num);
        }
        //解析模板
        return view('admin.partition.index',['partition'=>$partition,'request'=>$request->all()]);
    }

    /*
        显示修改分区页面
    */
    public function getEdit($id)
    {
        //查询所有用户
        $user = DB::table('user')->get();
        //根据id查询分区
        $partition = DB::table('partition')->where('id',$id)->first();

        //解析模板
        return view('admin.partition.edit',['partition'=>$partition,'user'=>$user]);
    }

    /*
        执行修改
    */
    public function postUpdate(Request $request)
    {
        //接收id
        $id = $request->input('id');
        //接收原来的值
        $a = $request->only(['repartition_name','reuser_id']);
        $aa = array_values($a);
        //接收新值
        $b = $request->only(['partition_name','user_id']);
        $bb = array_values($b);
        //判断用户是否修改需要修改
        if($aa == $bb){
            return redirect('/admin/partition/index')->with('success','分区修改成功');
        }

        //执行修改
        $res = Db::table('partition')->where('id',$id)->update($b);
        if($res){
            return redirect('/admin/partition/index')->with('success','分区修改成功');
        }else{
            return back()->with('error','分区修改失败');
        }
    }

    /*
        删除分区
    */
    public function postDelete(Request $request)
    {
        //接收id
        $id = $request->input('id');
        //判断分区下是否有子类
        $plate = DB::table('plate')->where('partition_id',$id)->first();
        if($plate){
            return 1;
        }

        //执行删除
        $res = DB::table('paritition')->where('id',$id)->delete();
        //判断
        if($res){
            return 2;
        }else{
            return 3;
        }
    }
}  
 